************************************************
* Sleep Project - Pedro Bessone, Gautam Rao, Heather Schofield, Frank Schilbach, and Mattie Toma
* Purpose: Replicates main paper, Figure 1 (Typical Sleep in Chennai)
* Last edited: 07 May 2021
************************************************

use "$d/sleep_examples_dataset.dta", replace

* Typical Night-Sleep (70%)

	preserve
	
	keep if pid==5572 & day_in_study==6

		rename sleeporawake asleep
		replace asleep = "1" if asleep == "S"
		replace asleep = "0" if asleep == "W"
		destring asleep, replace
		
	gen awake_2 = 1 if asleep==0
	replace awake_2 = 0 if asleep==1
	
	replace time_hr = time_hr + 24
	
	keep if sleep_time > prev_day_inbed_time & sleep_time < prev_day_outbed_time

			gen sleeping_run = 0
			
			bys pid date (time_hr): replace sleeping_run = sleeping_run[_n-1] + 1 if (sleep[_n] == 1 & sleep[_n-1] == 1)
			
			bys pid date (time_hr): gen streak_number = _n*(sleeping_run[_n+1]!=(sleeping_run[_n]+1))*(sleep[_n]==1)*(sleeping_run!=0) ///Streaks will be identified by a unique number.
			
			replace sleeping_run = sleeping_run + 1 ///Last line of code doesn't count first sleep - adding it back!
			
			egen group_pid_date = group(pid date)
			
			qui unique streak_number, by(group_pid_date) gen(number_of_streaks)
			replace number_of_streaks = number_of_streaks - 1 ///It also counts the zero which we don't want to count!
			
			bys pid date: egen average_sleep_length = mean(sleeping_run) if streak_number!=0
			
			bys pid date: egen longest_sleep_length = max(sleeping_run) if streak_number!=0
	
	
	twoway (spike asleep time_hr ,  lcolor("165 165 165") lwidth(thick)) ///
	(spike awake_2 time_hr ,  lcolor("205 15 15") lwidth(thick)), ///
	ylab(0(0)0, nolab tlength(0)) yticks(0(0)0, noticks) ysc(r(-0.05 1.1)) graphregion(fcolor(white)) xtitle("") xsc(r(24 33)) xline(24.55, lpattern(dash) lcolor("155 155 155")) xline(32.4, lpattern(dash) lcolor("155 155 155")) aspectratio(0.32) xsize(3.25) ysize(1) yscale(lstyle(none)) legend(order(1 "Sleeping" 2 "Awake") position(3) cols(1) rows(2) ring(9) region(lcolor(white)) textwidth(6.0) symxsize(*0.8) size(6))  xlabel(24 "0 AM" 26 "2 AM" 28 "4 AM" 30 "6 AM" 32 "8 AM", labsize(large)) plotregion(margin(0 0 0 0))

	graph export "$om/Figures/Figure1_typical_sleep.eps", replace	
	
	restore
	
* Good night-sleep
	
	preserve
		
	keep if pid==5006 & (date==21124 | date==21125)
		
		rename sleeporawake asleep
		replace asleep = "1" if asleep == "S"
		replace asleep = "0" if asleep == "W"
		destring asleep, replace
		
	gen awake_2 = 1 if asleep==0
	replace awake_2 = 0 if asleep==1
		
	replace time_hr = time_hr + 24 if time_hr >= 0 & time_hr<=10
		
	keep if (sleep_time > an_2_inbed_time & date==21124) | (sleep_time < prev_day_outbed_time & date==21125)
		
			gen sleeping_run = 0
				
			bys pid date (time_hr): replace sleeping_run = sleeping_run[_n-1] + 1 if (sleep[_n] == 1 & sleep[_n-1] == 1)
				
			bys pid date (time_hr): gen streak_number = _n*(sleeping_run[_n+1]!=(sleeping_run[_n]+1))*(sleep[_n]==1)*(sleeping_run!=0) ///Streaks will be identified by a unique number.
			///Note that this unique number isn't 1, 2, 3... but may be that the first streak is identified by 5, the next streak by 14, and so on
			///Nevertheless this allows us to do statistics at PID-date level
				
			replace sleeping_run = sleeping_run + 1 ///Last line of code doesn't count first sleep - adding it back!
				
			egen group_pid_date = group(pid date)
				
			qui unique streak_number, by(group_pid_date) gen(number_of_streaks)
			replace number_of_streaks = number_of_streaks - 1 ///It also counts the zero which we don't want to count!
				
			bys pid date: egen average_sleep_length = mean(sleeping_run) if streak_number!=0
				
			bys pid date: egen longest_sleep_length = max(sleeping_run) if streak_number!=0

		twoway (spike asleep time_hr ,  lcolor("165 165 165") lwidth(thick)) ///
		(spike awake_2 time_hr ,  lcolor("205 15 15") lwidth(thick)), ///
		ylab(0(0)0, nolab tlength(0)) yticks(0(0)0, noticks) ysc(r(-0.05 1.1)) graphregion(fcolor(white)) xtitle("")  xsc(r(23 32)) xline(23.52, lpattern(dash) lcolor("155 155 155")) xline(31.4, lpattern(dash) lcolor("155 155 155")) aspectratio(0.32) xsize(3.25) ysize(1) yscale(lstyle(none)) legend(order(1 "Sleeping" 2 "Awake") position(3) cols(1) rows(2) ring(9) region(lcolor(white)) textwidth(6.0) symxsize(*0.8) size(6))  xlabel(24 "0 AM" 26 "2 AM" 28 "4 AM" 30 "6 AM", labsize(large)) plotregion(margin(0 0 0 0))

		graph export "$om/Figures/Figure1_good_sleep.eps", replace	
		
		restore
